<?php

/**
 * Description of G_Db
 *
 * @author Monk <eoe2005@gmail.com>
 */
class G_Db {

    protected static $_dbs = array();

    public static function init($conf) {
        $key = md5($conf);
        //var_dump($key,isset(self::$_dbs[$key]));
        if (isset(self::$_dbs[$key]) === false) {
            $index = strpos($conf, "://");
            $attr = array();
            if ($index !== false) {
                $drive = strtolower(trim(substr($conf, 0, $index)));
                $conf = substr($conf, $index + 3);
                $confs = explode(';', $conf);
                foreach ($confs AS $item) {
                    $index = strpos($item, '=');
                    if ($index !== false) {
                        $attr[trim(strtolower(substr($item, 0, $index)))] = trim(substr($item, $index + 1));
                    }
                }
                if ($drive == 'mysql') {
                    include G_ROOT . 'Db' . DS . 'G_Mysql.cls.php';
                    self::$_dbs[$key] = new G_Mysql($attr);
                } elseif ($drive == 'mysqli') {
                    include G_ROOT . 'Db' . DS . 'G_Mysqli.cls.php';
                    self::$_dbs[$key] = new G_Mysqli($attr);
                } elseif ($drive == 'pdo') {
                    include G_ROOT . 'Db' . DS . 'G_Pdo.cls.php';
                    self::$_dbs[$key] = new G_Pdo($attr);
                } elseif ($drive == 'sqlite') {
                    include G_ROOT . 'Db' . DS . 'G_Sqlite.cls.php';
                    self::$_dbs[$key] = new G_Sqlite($attr);
                } elseif ($drive == 'sqlite3') {
                    include G_ROOT . 'Db' . DS . 'G_Sqlite3.cls.php';
                    self::$_dbs[$key] = new G_Sqlite3($attr);
                } elseif ($drive == 'postsqlgre') {
                    include G_ROOT . 'Db' . DS . 'G_Postgresql.cls.php';
                    self::$_dbs[$key] = new G_Postgresql($attr);
                } else {
                    trigger_error('数据库驱动错误');
                    die();
                }
            } else {
                trigger_error('数据库配置错误');
                die();
            }
        } else {
            trigger_error('数据库配置错误');
            die();
        }
        return self::$_dbs[$key];
    }

}

abstract class GC_Db {

    /**
     * 执行更新操作
     */
    public abstract function exec($sql);

    /**
     * 执行查询操作
     */
    public abstract function query($sql);

    /**
     * 转义字符串
     */
    public abstract function escape($str);

    /**
     * 开始事务
     */
    public abstract function begin();

    /**
     * 提交事务
     */
    public abstract function commit();

    /**
     * 回滚事务
     */
    public abstract function rollback();

    public abstract function getVar($sql);

    /**
     * 获取一行数据
     */
    public abstract function getRow($sql);

    /**
     * 返回查询结果
     */
    public abstract function getResults($sql);

    /**
     * 获取最后插入的id
     */
    public abstract function getLastId();

    /**
     * 获取更新影响的行数
     */
    public abstract function affectedRows();

    /**
     * 插入数据
     * @param type $table
     * @param type $data
     */
    public function insert($sql) {
        if ($this->exec($sql) !== FALSE) {
            return $this->getLastId();
        }
        return 0;
    }

    /**
     * 
     * 更新数据
     */
    public function update($sql) {
        if ($this->exec($sql)) {
            return $this->affectedRows();
        }
        return false;
    }

    /**
     * 删除数据
     * @param type $sql
     * @return boolean
     */
    public function delete($sql) {
        if ($this->exec($sql)) {
            return $this->affectedRows();
        }
        return false;
    }

}
